package com.designPattern.javaSupport;

import java.util.Observable;
import java.util.Observer;

import com.designPattern.observer.observerInter.DisplayElement;

/**
 * 实现Java内置的Observer接口和布告板显示接口
 */
public class CurrentConditionDisplay implements Observer, DisplayElement {
	
	Observable observable;
	private float temperature;//温度
	private float humidity;//湿度
	
	/**
	 * 使用被观察者构造，将自己加入观察者列表
	 * @param observable
	 */
	public CurrentConditionDisplay(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
	}

	@Override
	public void display() {
		System.out.println("当前状况为：温度" + temperature + "，湿度" + humidity);
	}

	@Override
	public void update(Observable o, Object arg) {
		if(o instanceof WeatherData){
			WeatherData weatherData = (WeatherData)o;
			this.humidity = weatherData.getHumidity();
			this.temperature = weatherData.getTemperature();
			display();
		}
	}

}
